package com.sinodata.bsm.cicp.collector.tuxedo;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.CommandReader;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * <p>Title: TuxedoQueueFullCollector</p>
 * <p>Description: 采集Tuxedo的达到满标准的队列信息</p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: Beijing youlian chuangxin system integration CO.,LTD</p>
 *
 * @author credible_zhang
 * @version 1.0
 */
public class TuxedoQueueFullCollector extends Collector {
    //定义采集程序中所使用的命令
    public static final String COMMAND = "tuxedoQcnt.sh";
    //定义超时时间
    public static final int TIMEOUT = 10;
    //定义Log4j
    public static Logger logger = Logger.getLogger(TuxedoQueueFullCollector.class);

    /**
     * 采集程序实体
     * @param resAtt ResAttributeValValue[]
     * @param ip String
     * @return List
     * @throws CollectorException 
     */

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        String cmd = this.paramMap.get("采集命令");
        String pattern = "\\s*(\\S+)\\s+(\\S+)\\s+(\\d+)\\s*";
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        BufferedReader reader = null;
        try {
            reader = CommandReader.getReader(ip, cmd, (int) this.interval, 10);
            String line = reader.readLine();
            // StringBuffer valueBuffer = new StringBuffer("");
            ResPropertyValue propertyValueX = new ResPropertyValue();
            ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
            //      List value = new ArrayList();
            while (line != null) {
                //        String line2 = line.replaceAll("\t","^_^");
                //        String[] subStr = StringUtil.splitStr(line2," ");
                Pattern pattn = Pattern.compile(pattern);

                Matcher match = pattn.matcher(line);
                if (match.matches()) {
                    propValues[0] = ResPropertyValue.createPropValue(match.group(1), propMetas[0]);
                    propValues[1] = ResPropertyValue.createPropValue(match.group(2), propMetas[1]);
                    propValues[2] = ResPropertyValue.createPropValue(match.group(3), propMetas[2]);

                }
                String temp = reader.readLine();
                propertyValueX.setValues(propValues);
                values.add(propertyValueX);
                line = temp;
            }
            if (values.size() == 0) {
                return null;
            }
            return values;
        } catch (IOException ioe) {
            logger.error("Collect tuxedo queue error.", ioe);
            throw new CollectorException(ioe);
        }
    }

    /**
     * 构造采集命令
     * @param resAtt ResAttributeValValue[]
     * @return String
     */
    @Override
    public String[] getParamMeta() {
        return new String[] { "采集命令" };
    }

    @Override
    public String[] getPropMeta() {
        return new String[] { "队列名", "服务名", "积压数量" };
    }
}
